package org.springblade.oa.controller;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.oa.entity.Message;
import org.springblade.oa.service.IMessageService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("msg")
@AllArgsConstructor
public class MessageController {
	/*
	 * 消息提醒
	 * zhangxl 2020-02-04
	 */
	private IMessageService imsg;
	/**
	 * 获取消息总数
	 * zhangxl 2020-02-05
	 */
	@RequestMapping("/getMsgCount")
	@ApiOperation(value = "获取消息总数", notes = "获取消息总数")
	public R getMsgCount(BladeUser user) {
//		localhost:8200/msg/getMsgCount
//		localhost/blade-oa/msg/getMsgCount

		//返回数据
		Map<String, Object> rmap = new HashMap<>();

		try {
			rmap = imsg.getMsgCount(user);
		} catch (Exception e) {
			e.printStackTrace();
			return R.data(200, rmap, "exception:" + e.getMessage());
		}
		return R.data(rmap);
	}

	/**
	 * 获取消息
	 * zhangxl 2020-02-05
	 * type 1:待办任务，2：消息提醒
	 */
	@RequestMapping("/getMsg")
	@ApiOperation(value = "获取消息", notes = "获取消息")
	public R getMsg(BladeUser user, String type, String page, String pageSize) {
//		localhost:8200/msg/getMsg?type=1&page=1&pageSize=10
//		localhost/blade-oa/msg/getMsg?type=1&page=1&pageSize=10

		//返回数据
		List<Map<String, Object>> rlst = new ArrayList<>();

		try {
			if (type == null || "".equals(type))
				return R.data(200, rlst, "消息类型不能为空（type 1.代办任务 2.消息 3.系统通知）");
			rlst = imsg.getMsg(user, type, getLimitStr(pageSize, page));
		} catch (Exception e) {
			e.printStackTrace();
			return R.data(200, rlst, "exception:" + e.getMessage());
		}
		return R.data(rlst);
	}

	/**
	 * 消息 标记已读
	 * zhangxl 2020-02-06
	 */
	@RequestMapping("/falgMsgYD")
	@ApiOperation(value = "获取消息", notes = "获取消息")
	public R falgMsgYD(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		//返回数据
		boolean falg = false;
		List<Integer> xxids = Func.toIntList(ids);
		for (int i = 0; i <xxids.size(); i++) {
			Integer id = xxids.get(i);
			try {
				if (id == null || "".equals(id))
				return R.data(200, falg, "消息id不能为空！！！");
				Message msge = imsg.getMsgById(id);
				msge.setSfyd("1");//是否已读（0：否，1：是）
				msge.setId(id);
				falg = imsg.update(msge);
			} catch (Exception e) {
				e.printStackTrace();
				return R.data(200, falg, "exception:" + e.getMessage());
			}
			return R.data(falg);
		}
		return  null;
	}
	/**
	 * 消息 标记已读
	 * zhangxl 2020-02-06
	 */
	@RequestMapping("/falgAllMsgYD")
	@ApiOperation(value = "获取消息", notes = "获取消息")
	public R falgAllMsgYD(BladeUser user) {
		//返回数据
		boolean falg = false;
			try {
				Message message = new Message();
				message.setJsrfw(user.getUserId()+"");//角色范围
				List<Message> messagesList;
				messagesList = imsg.list(Condition.getQueryWrapper(message));
				if (messagesList.size()>0){
					for (int i = 0; i <messagesList.size() ; i++) {
						Message me = messagesList.get(i);
						Message msge = imsg.getMsgById(me.getId());
						msge.setSfyd("1");//是否已读（0：否，1：是）
						msge.setId(me.getId());
						falg = imsg.update(msge);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				return R.data(200, falg, "exception:" + e.getMessage());
			}
			return R.data(falg);
	}

	/**
	 * 根据 页码、页大小 获取分页字符串 " LIMIT 1,10 "
	 * zhangxl 2019-09-10
	 */
	public static String getLimitStr(String size, String page) {
		String str = "";

		try {
			if (!"".equals(size) && !"".equals(page)) {
				int pg = Integer.parseInt(page);
				int sz = Integer.parseInt(size);
				Integer num = (pg - 1) * sz;

				str = " LIMIT " + num + "," + size + "";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return str;
	}

	/**
	 * 根据 总条数、页大小 获取总页数
	 * zhangxl 2019-09-10
	 */
	public static Integer pageCount(int count, String size) {
		Integer totalPage = 0;
		try {
			if (0 != count && !"".equals(size)) {
				int sz = Integer.parseInt(size);

				Integer n = count % sz;
				if (n == 0) {
					totalPage = count / sz;
				} else {
					totalPage = count / sz + 1;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return totalPage;
	}

}
